/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* ReflectionUtil.java
*
* Created on December 1, 2007, 7:04 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
 */
package javautil.lang;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 *
 * @author stewari1
 */
public class ReflectionUtil {

    /** Creates a new instance of ReflectionUtil */
    public ReflectionUtil() {}

    /**
     * returns the class (without the package if any)
     * Note: c.getName() gives the names of the class with the full package.
     */
    public static String getClassName(Class c) {

        String FQClassName = c.getName();
        int firstChar;

        firstChar = FQClassName.lastIndexOf('.') + 1;

        if (firstChar > 0) {
            FQClassName = FQClassName.substring(firstChar);
        }

        return FQClassName;
    }

    /**
     * returns the package without the classname, empty string if
     * there is no package
     *
     * @param c description
     *
     * @return description
     */
    private static String getPackageName(Class c) {

        String fullyQualifiedName = c.getName();
        int lastDot               = fullyQualifiedName.lastIndexOf('.');

        if (lastDot == -1) {
            return "";
        }

        return fullyQualifiedName.substring(0, lastDot);
    }

    /**
     * invoke getter (javabean convention "get" + capitalizeFirstLetter(property)
     * on the object <CODE>obj</CODE> using reflection on <CODE>obj</CODE>. if
     * getter is not available silently return <CODE>null</CODE>.
     */
    public static Object getPropertyValue(Object obj, String property) {

        Object returnObj  = null;
        String methodName = "get" + StringUtil.capFirstLetter(property);
        Method method     = null;

        try {

            method    = obj.getClass().getMethod(methodName, (Class[]) null);
            returnObj = method.invoke(obj);

        } catch (NoSuchMethodException | SecurityException | IllegalAccessException
                 | IllegalArgumentException | InvocationTargetException ex) {}
        finally {
            return returnObj;
        }
    }

    public static void main(String[] args) {
        System.out.println(getPackageName("".getClass()));
    }
}
